import {
  query as queryUsers,
  cardlist,
  creatcard,
  removecard,
  cardDetail,
} from '../services/card';
import { routerRedux } from 'dva/router'
export default {
  namespace: 'card',

  state: {
    list: [],
    currentCard: [],
    currentDetail: {},
  },

  effects: {
    *getCardlist(_, { call, put }) {
      const response = yield call(cardlist);
      console.log(response);
      yield put({
        type: 'saveCurrentCard',
        payload: response,
      });
    },
    *carddetail({ payload: id }, { call, put }) {
      const response = yield call(cardDetail, id);
      yield put({
        type: 'saveDetailCard',
        payload: response,
      });
    },

    *create({ payload: values }, { call, put }) {
      const response = yield call(creatcard, values);
      if(response != ''){
        yield put(routerRedux.push('/people/readcard'));
      }
      yield put({ type: 'getCardlist' });
    },
    *remove({ payload: id }, { call, put }) {
      yield call(removecard, id);
      yield put({ type: 'getCardlist' });
    },
  },
  reducers: {
    /*     save(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    }, */
    saveCurrentCard(state, action) {
      return {
        ...state,
        currentCard: action.payload,
      };
    },
    saveDetailCard(state, action) {
      return {
        ...state,
        currentDetail: action.payload || {},
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentCard: {
          ...state.currentCard,
          notifyCount: action.payload,
        },
        currentDetail: {
          ...state.currentDetail,
          notifyCount: action.payload,
        },
      };
    },
  },
};
